//
//  SecondStyleCellForOnce.m
//  A段项目
//
//  Created by dllo on 16/1/30.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "SecondStyleCellForOnce.h"
#import "HomePageCell.h"
#import <UIImageView+WebCache.h>
#import "Model.h"
#import "NetWorkingHandler.h"
#import "TouchFirstView.h"
#import "ViewController.h"

@interface SecondStyleCellForOnce () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) UICollectionView *collection;

@end

@implementation SecondStyleCellForOnce


- (void)dealloc {
    
    [_collection release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        
        //  NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0|5|10 inSection:0];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        
        //  flowLayout.itemSize = CGSizeMake(80,80);
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 5, 10, 5);
        flowLayout.minimumLineSpacing = 10;
       
        
        self.collection = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
        self.collection.showsHorizontalScrollIndicator = NO;
        
        [self.contentView addSubview:self.collection];
        
        [self.collection registerClass:[HomePageCell class] forCellWithReuseIdentifier:@"pool1"];
        
    }
    
    return self;
    
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    [self.collection reloadData];
    
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.frame = self.bounds;
    self.collection.backgroundColor = [UIColor whiteColor];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
    
    return self.arrData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    Model *model = [[Model alloc] init];
    
    [model setValuesForKeysWithDictionary:self.arrData[indexPath.item]];
    
 
    
    HomePageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.imageUrl]];
    
    [model release];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return CGSizeMake((self.frame.size.height - 30)/2, (self.frame.size.height - 30)/2);
}

/**点击 item 的代理方法. */

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    NSDictionary *dic = [self.arrData objectAtIndex:indexPath.row];
    
    
    Model *model = [[Model alloc] init];
    
    [model setValuesForKeysWithDictionary:dic];
    
    NSString *str = [model.linkData substringFromIndex:9];
    
    NSString *url = [NSString stringWithFormat:@"http://mapp.tiankong.com/imageUser/detail/v1.1?imageId=%@&imageType=1",str];
    
  
    [NetWorkingHandler netWorkingWithUrl:url complete:^(id result) {
        
        NSDictionary *dic = [result objectForKey:@"data"];
        
        Model *model = [[Model alloc] init];
        
        [model setValuesForKeysWithDictionary:dic];
        
        
        
    }];
    
    NSString *str1 = [NSString stringWithFormat:@"%@",self.dicOfType];
    
    NSString *codeStr = [str1 stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [NetWorkingHandler netWorkingWithUrl:[NSString stringWithFormat:@"http://mapp.tiankong.com/search/similar?key=%@&pageNum=1&pageSize=50&tag=%@",self.dicOfType,codeStr] complete:^(id result) {
        
        id object = [self nextResponder];
        while(![object isKindOfClass:[ViewController class]]&&object != nil ) {
            
            object = [object nextResponder];
            
            
        }
        
        ViewController *VC = (ViewController *)object;
        
        TouchFirstView *touchView = [[TouchFirstView alloc] init];
        
        [touchView setModalTransitionStyle:2];
        [touchView setModalPresentationStyle:1];
        
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:result];
        
#pragma mark 收藏部分
        
        NSString *str = [NSString stringWithString:model.imageUrl];
        
        NSDictionary *dic1 = [NSDictionary dictionaryWithObject:str forKey:@"url"];
        
        [touchView.array arrayByAddingObject:dic1];
        
        
        
        touchView.array = [NSMutableArray arrayWithArray:[dic objectForKey:@"data"]];
        
        [touchView.array insertObject:dic1 atIndex:0];
        
        
        [VC presentViewController:touchView animated:YES completion:^{
            
            
        }];
        
        
        [touchView release];
        
        
        
    }];
    
    [model release];

    
    
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
